﻿namespace Trader.Runner
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media;
    using BO.Interface;
    using Indicators.Interface;
    using Swordfish.WPF.Charts;

    /// <summary>
    /// Interaction logic for SingleStock.xaml
    /// </summary>
    public partial class SingleStock : Window
    {
        private readonly ISignal signal;
        private bool hasOpenedLink;

        public SingleStock(IStock stock, ISignal signal, DateTime buyDate, DateTime sellDate)
        {
            this.signal = signal;
            InitializeComponent();

            Title = stock.Name;

            var line = new ChartPrimitive {Color = Colors.Red, Label = "Signal Test Period"};
            for (DateTime valueDate = buyDate - TimeSpan.FromDays(signal.TestDays);
                 valueDate <= buyDate;
                 valueDate += TimeSpan.FromDays(1))
            {
                line.AddPoint((valueDate - (buyDate - TimeSpan.FromDays(signal.TestDays))).TotalDays,
                              (double) stock.Quotes[valueDate].Closing);
            }
            xyLineChart.Primitives.Add(line);

            line = new ChartPrimitive {Color = Colors.Blue, Label = "Hold Period"};
            double holdDays = (sellDate - buyDate).TotalDays;
            for (int i = 0; i <= holdDays; i++)
            {
                line.AddPoint(i + signal.TestDays, (double) stock.Quotes[buyDate + TimeSpan.FromDays(i)].Closing);
            }
            xyLineChart.Primitives.Add(line);

            line = new ChartPrimitive {Color = Colors.Green, Label = "After Sell Period"};
            for (int i = 0; i <= 7; i++)
            {
                line.AddPoint(i + signal.TestDays + holdDays,
                              (double) stock.Quotes[sellDate + TimeSpan.FromDays(i)].Closing);
            }
            xyLineChart.Primitives.Add(line);

            xyLineChart.SubNotes = new[]
                                       {
                                           string.Format("Signal generated by - {0}", signal.Name),
                                           "Double click graph to view documentation"
                                       };

            xyLineChart.RedrawPlotLines();

            xyLineChart.MouseDoubleClick += xyLineChart_MouseDoubleClick;
        }

        private void xyLineChart_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (hasOpenedLink)
            {
                return;
            }

            hasOpenedLink = true;

            OpenDocumentationLink();
        }

        private void OpenDocumentationLink()
        {
            WindowState = WindowState.Maximized;

            var navigator = new WebBrowser();
            navigator.Navigate(signal.URL);
            navigator.VerticalAlignment = VerticalAlignment.Stretch;
            navigator.HorizontalAlignment = HorizontalAlignment.Stretch;
            panel.Children.Add(navigator);
        }
    }
}